# coding: utf-8
import base64
import hashlib
from pathlib import Path
from typing import Union

import rsa

from common import pytest_config, timestamp


def create_rsa_key_pair():
    """
    创建RSA密钥对
    :return:
    """
    variables_path = Path(pytest_config.variables)
    public_key, private_key = rsa.newkeys(1024)
    with open(variables_path / 'public.pem', 'w+') as fp:
        fp.write(public_key.save_pkcs1().decode())
    with open(variables_path / 'private.pem', 'w+') as fp:
        fp.write(private_key.save_pkcs1().decode())


class DataToolsUtil:
    @staticmethod
    def md5_encode(data: str, encoding='utf-8'):
        data = str(data).encode(encoding)
        md5 = hashlib.md5(data)
        return md5.hexdigest()

    @staticmethod
    def base64_encode(data: str, encoding='utf-8'):
        data = str(data).encode(encoding)
        return base64.b64encode(data).decode(encoding)

    @staticmethod
    def rsa_encrypt(public_key: str, data: str, encoding='utf-8'):
        """
        rsa加密
        :param public_key:
        :param data:
        :param encoding:
        :return:
        """
        pass

    @staticmethod
    def timestamp(length: Union[str, int] = 13):
        """
        获取当前时间戳
        :param length: 时间戳长度，默认13位
        :return:
        """
        return timestamp(length)
